/*
 *  txdnet.cn tonydon
 * 
 */
package cn.txdnet.txweb.utils;

import cn.txdnet.txcms.domain.Catalogs;
import cn.txdnet.txcms.dto.TagsDTO;
import cn.txdnet.txcms.service.CatalogsLevelService;
import cn.txdnet.txcms.service.CatalogsService;
import cn.txdnet.txcms.service.InfotagsService;
import java.util.List;
import txdnet.util.DataUtil;
import txdnet.util.StaticMark;

/**
 *
 * @author txdnet
 */
public class TaglibUtil {

    /**
     * 将时间毫秒转为日期格式
     *
     * @param timeMillis
     * @param displaySize
     * @return
     */
    public static String formatDate(Long timeMillis, Integer displaySize) {
        return DataUtil.getTimestamp(timeMillis).toString().substring(0, displaySize);
    }

    /**
     * 将info origin来源格式化为链接 如 噢啦网|http://www.uuola.com/
     *
     * @param origin
     * @return
     */
    public static String formatInfoOrigin(String origin) {
        if (StringUtil.isEmpty(origin)) {
            return StaticMark.STR_EMPTY;
        }
        int pos = origin.indexOf(StaticMark.CHAR_VERTICAL_LINE);
        if (pos > 0) {
            return "<a target=\"_blank\" href=\"" + origin.substring(pos + 1) + "\">" + origin.substring(0, pos) + "</a>";
        }
        return origin;
    }

    /**
     * 将ctp转为导航链接
     * @param ctp
     * @return 
     */
    public static String getInfoCatNav(String ctp, String navTpl) {
        if(StringUtil.isEmpty(ctp)){
            return StaticMark.STR_EMPTY;
        }
        return CatalogsLevelService.getLevelNavigationByCtp(ctp, navTpl);
    }
    
    /**
     * 得到info 当前类目链接
     * @param cid
     * @return 
     */
    public static String getInfoCurrCatNav(Integer cid, String navTpl){
        Catalogs cat = CatalogsService.getCatalogsByCid(cid);
        if(cat == null){
            return StaticMark.STR_EMPTY;
        }
        return navTpl.replace("$cid",Integer.toString(cid)).replace("$cname",cat.getCname());
    }
    
    /**
     * 得到info tags 链接
     * @param infoid
     * @return 
     */
    public static String getInfoTagsNav(Long infoid, String navTpl){
        List<TagsDTO> infoTagsList = InfotagsService.queryInfoTagsForView(infoid);
        if(CollectionUtil.isEmpty(infoTagsList)){
            return StaticMark.STR_EMPTY;
        }
        StringBuilder sb = new StringBuilder(navTpl.length());
        for(TagsDTO dto : infoTagsList){
            sb.append(navTpl.replace("$tagid", dto.getTagid().toString())
                    .replace("$tagname", dto.getTagname()));
        }
        return sb.toString();
    }
    

    /**
     * 根据原始图片地址和后缀标识，得到缩图地址,如果没有图片返回一个缺省图片地址
     * @param src
     * @param suff
     * @param defaultSrc
     * @return 
     */
    public static String getResizeImgUrl(String src, String suff, String defaultSrc){
        if(StringUtil.isEmpty(src) || src.length()<9){
            return defaultSrc;
        }
        if(StringUtil.isEmpty(suff)){
            return src;
        }
        int pos = src.lastIndexOf(StaticMark.CHAR_DOT);
        String orgImgName = src.substring(0, pos);
        String orgExtName = src.substring(pos);
        return orgImgName.concat(StaticMark.STR_UNDER_LINE).concat(suff).concat(orgExtName);
    }
}